import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:travel/components/home/index.dart';
import 'package:travel/components/home/message.dart';
import 'package:travel/components/home/mine.dart';
import 'package:travel/components/home/video.dart';
import 'package:travel/event/index.dart';
import 'package:travel/utils/colors.dart';
import 'package:travel/utils/styles.dart';
import 'package:travel/utils/tools.dart';
import 'package:travel/screen/flutter_screenutil.dart';

class HomeController extends GetxController {
  Rx<int> index = Rx(0);

  late PageController pageController;

  late StreamSubscription stream;

  @override
  void onInit() {
    pageController = PageController(initialPage: index.value);
    stream = eventBus.on<HomeChangeIndexEvent>().listen((event) {
      index.value = event.index;
      // gotoView(event.index);
    });
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
    stream.cancel();
    pageController.dispose();
  }

  void gotoView(int index) {
    pageController.animateToPage(index,
        duration: const Duration(milliseconds: 500), curve: Curves.linear);
  }

  // @override
  // void onReady() {
  //   super.onReady();
  //   Future.delayed(const Duration(seconds: 5), () {
  //     Get.toNamed(RoutePath.homeAuth);
  //   });
  // }
}

class HomeView extends GetView<HomeController> {
  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: createPageView(context),
        resizeToAvoidBottomInset: true,
        bottomNavigationBar: Obx(() {
          return BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            currentIndex: controller.index.value,
            elevation: 0,
            iconSize: 18.sp,
            backgroundColor: backgroundColor,
            unselectedItemColor:
                ColorsUtils.parseColorFromHexString(ColorsUtils.color6666),
            unselectedLabelStyle: StylesUtils.customTextStyle(
                fontSize: 12.sp, color: ColorsUtils.color6666),
            selectedItemColor:
                ColorsUtils.parseColorFromHexString(ColorsUtils.color3478),
            selectedLabelStyle: StylesUtils.customTextStyle(
                fontSize: 12.sp, color: ColorsUtils.color3478),
            items: [
              BottomNavigationBarItem(
                  icon: ToolsUtils.assetImageSvg('u246.svg',
                      width: 18.w, height: 18.h),
                  activeIcon: ToolsUtils.assetImageSvg('u246_selected.svg',
                      width: 18.w, height: 18.h),
                  label: '首页'),
              BottomNavigationBarItem(
                  icon: ToolsUtils.assetImageSvg('u243.svg',
                      width: 18.w, height: 18.h),
                  activeIcon: ToolsUtils.assetImageSvg('u243_selected.svg',
                      width: 18.w, height: 18.h),
                  label: '视频'),
              BottomNavigationBarItem(
                  icon: ToolsUtils.assetImageSvg('u240.svg',
                      width: 18.w, height: 18.h),
                  activeIcon: ToolsUtils.assetImageSvg('u240_selected.svg',
                      width: 18.w, height: 18.h),
                  label: '消息'),
              BottomNavigationBarItem(
                  icon: ToolsUtils.assetImageSvg('u237.svg',
                      width: 18.w, height: 18.h),
                  activeIcon: ToolsUtils.assetImageSvg('u237_selected.svg',
                      width: 18.w, height: 18.h),
                  label: '我的'),
            ],
            landscapeLayout: BottomNavigationBarLandscapeLayout.centered,
            onTap: (int index) {
              if (index != controller.index.value) {
                controller.index.value = index;
                controller.gotoView(index);
              }
            },
          );
        }));
  }

  Color get backgroundColor {
    if (controller.index.value == 1) {
      return ColorsUtils.rgba(17, 17, 17, 1);
    }
    return Colors.white;
  }

  PageView createPageView(BuildContext context) {
    return PageView(
      physics: const NeverScrollableScrollPhysics(),
      scrollBehavior: const ScrollBehavior(),
      controller: controller.pageController,
      children: const [IndexView(), VideoPage(), MessageView(), MineView()],
    );
  }
}

class HomeViewSate extends StatefulWidget {
  const HomeViewSate({super.key});

  @override
  State<HomeViewSate> createState() => _HomeViewSateState();
}

class _HomeViewSateState extends State<HomeViewSate> {
  int index = 0;

  late PageController pageController;

  late StreamSubscription stream;

  @override
  void initState() {
    pageController = PageController(initialPage: index);
    stream = eventBus.on<HomeChangeIndexEvent>().listen((event) {
      setState(() {
        index = event.index;
        // gotoView(event.index);
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    stream.cancel();
    pageController.dispose();
    super.dispose();
  }

  void gotoView(int index) {
    pageController.animateToPage(index,
        duration: const Duration(milliseconds: 500), curve: Curves.linear);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: createPageView(context),
        resizeToAvoidBottomInset: true,
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: index,
          elevation: 0,
          iconSize: 18.sp,
          backgroundColor: backgroundColor,
          unselectedItemColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.color6666),
          unselectedLabelStyle: StylesUtils.customTextStyle(
              fontSize: 12.sp, color: ColorsUtils.color6666),
          selectedItemColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.color3478),
          selectedLabelStyle: StylesUtils.customTextStyle(
              fontSize: 12.sp, color: ColorsUtils.color3478),
          items: [
            BottomNavigationBarItem(
                icon: ToolsUtils.assetImageSvg('u246.svg',
                    width: 18.w, height: 18.h),
                activeIcon: ToolsUtils.assetImageSvg('u246_selected.svg',
                    width: 18.w, height: 18.h),
                label: '首页'),
            BottomNavigationBarItem(
                icon: ToolsUtils.assetImageSvg('u243.svg',
                    width: 18.w, height: 18.h),
                activeIcon: ToolsUtils.assetImageSvg('u243_selected.svg',
                    width: 18.w, height: 18.h),
                label: '视频'),
            BottomNavigationBarItem(
                icon: ToolsUtils.assetImageSvg('u240.svg',
                    width: 18.w, height: 18.h),
                activeIcon: ToolsUtils.assetImageSvg('u240_selected.svg',
                    width: 18.w, height: 18.h),
                label: '消息'),
            BottomNavigationBarItem(
                icon: ToolsUtils.assetImageSvg('u237.svg',
                    width: 18.w, height: 18.h),
                activeIcon: ToolsUtils.assetImageSvg('u237_selected.svg',
                    width: 18.w, height: 18.h),
                label: '我的'),
          ],
          landscapeLayout: BottomNavigationBarLandscapeLayout.centered,
          onTap: (int i) {
            setState(() {
              if (index != i) {
                index = i;
                gotoView(index);
              }
            });
          },
        ));
  }

  Color get backgroundColor {
    if (index == 1) {
      return ColorsUtils.rgba(17, 17, 17, 1);
    }
    return Colors.white;
  }

  PageView createPageView(BuildContext context) {
    return PageView(
      physics: const NeverScrollableScrollPhysics(),
      scrollBehavior: const ScrollBehavior(),
      controller: pageController,
      children: const [IndexView(), VideoPage(), MessageView(), MineView()],
    );
  }
}
